package cn.hxzy.controller;


import cn.hxzy.domain.ClassTable;
import cn.hxzy.domain.User;
import cn.hxzy.domain.vo.ClassStudentVo;
import cn.hxzy.domain.vo.StudentVo;
import cn.hxzy.domain.vo.StudentVo2;
import cn.hxzy.response.R;
import cn.hxzy.response.StatusEnum;
import cn.hxzy.service.TeacherService;
import cn.hxzy.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;
//班级和学生控制器
@RestController
@RequestMapping("/teacher")
public class ClassroomController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 创建班级
     * @param clazzName  班级名称
     * @return
     */
    @PostMapping("/createClassroom")
    public R createClassroom(@RequestParam("className") String clazzName) {
        int i = teacherService.selectOneClass(clazzName);
        if (i!=0) {
            return R.error(StatusEnum.PARAM_INVALID);
        }
        User user = ThreadLocalUtil.getUser();
        ClassTable classTable = new ClassTable();
        classTable.setCreateId(user.getUserId().intValue());
        classTable.setClassName(clazzName);
        classTable.setCreateTime(LocalDateTime.now());
        classTable.setSchoolId(user.getSchoolId());
        boolean classroom = teacherService.createClassroom(classTable);
        if (classroom){
            return R.success("创建班级成功");
        }
        return R.error(StatusEnum.PARAM_INVALID);
    }
    /**
     * 通过教师id查询班级
     */
    @GetMapping("/findGradeByTeacherId")
    public R<List<ClassTable>> findClassroomByTeacherId(@RequestParam("id") Integer id) {
        List<ClassTable> classroomByTeacherId = teacherService.findClassroomByTeacherId(id);
        return R.success(classroomByTeacherId);
    }

    /**
     * 根据班级Id修改班级名称
     * @param classId
     * @param className
     * @return
     */
    @PutMapping("/updateClassroom")
    public R updateClassroom(Integer classId,String className) {
        String s = teacherService.updateClass(classId, className);
        return R.success("修改成功");
    }

    /**
     * 删除班级
     * @param classroomId
     * @return
     */
    @DeleteMapping("deleteClassroom/{classroomId}")
    public R deleteClassroom(@PathVariable Long classroomId) {
        Boolean bool = teacherService.deleteClassroom(classroomId);
        if (bool) {
            return R.success(classroomId);
        } else {
            return R.error(StatusEnum.SERVICE_ERROR);
        }

    }
    /**
     * 模糊查询班级
     * @param className  班级名称
     */
    @GetMapping("/findClassroomBysearch")
    public R<List<ClassTable>> findClassroomByName(String className) {
        List<ClassTable> classroomByName = teacherService.findClassroomByName(className);
        if (classroomByName.size() > 0) {
            return R.success(classroomByName);
        }
        return R.error(StatusEnum.FORBIDDEN);
    }



    /**
     * 获取账户列表。
     * 支持通过班级名、专业名、入学年份筛选
     *
     * @param className   班级名称，可选参数
     * @param majorName   专业名称，可选参数
     * @param admissionYear 入学年份，可选参数
     * @return 账户信息列表
     */
    @GetMapping("/listStudentBySerach")
    public R<List<User>> listAccounts(
            @RequestParam(value = "class", required = false) Integer className,
            @RequestParam(value = "major", required = false) Integer majorName,
            @RequestParam(value = "admissionYear", required = false) Integer admissionYear) {
        System.out.println(className+"==="+majorName+"==="+admissionYear);
        List<User> users = teacherService.listAccounts(className, majorName, admissionYear);
        if (users.size() > 0) {
            return R.success(users);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 更新指定学生的ID的账户信息
     * @param studentVo 更新后的账户对象
     */
    @PutMapping("/updateStudent")
    public R updateStudent( @RequestBody StudentVo studentVo) {
        return teacherService.updateStudent(studentVo);
    }

    /**
     * 删除指定ID的学生账户
     *
     * @param userId 要删除的账户ID
     */
    @DeleteMapping("deleteStudent/{id}")
    public R deleteAccount(@PathVariable("user_id") Long userId) {
        User users = teacherService.deleteStudent(userId);
        if (users != null) {
            return R.success(userId);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 导入学生到班级
     * @param classroomId  班级的唯一标识符
     * @param file   包含学生信息的文件
     * @return
     */
    @PostMapping("/students/import/{classroomId}")
    public R importStudents(@PathVariable Long classroomId, @RequestParam("file") MultipartFile file) {
        int i = teacherService.importStudents(classroomId, file);
        if (i > 0) {
            return R.success(i);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    /**
     * 导出班级内的学生列表
     * @param classroomId
     * @return
     */
    @GetMapping("/students/export1")
    public R<MultipartFile> exportStudents(@PathVariable Long classroomId) {
        MultipartFile multipartFile = teacherService.exportStudents(classroomId);
        if (multipartFile != null) {
            return R.success(multipartFile);
        }
        return R.error(StatusEnum.SERVICE_ERROR);
    }

    @GetMapping("/students/export")
    public List<StudentVo2>getStudentByTeacher(@RequestParam("userType") String userType){
        return teacherService.selectStudentByTeacher(userType);
    }


    /*
     *  查询班级列表
     **/
    @GetMapping("/selectAllClassList")
    public R<List<ClassStudentVo>> selectAllClassList(@RequestParam(required = true) Integer courseId) {
        List<ClassStudentVo> allClassList = teacherService.selectAllClassList(courseId);
        return R.success(allClassList);
    }


    /**
     * 通过课程id和班级名称  班级创建人  以及是否授权模糊查询按班级列表
     */
    @GetMapping("/selectClassBySearchKey")
    public R<List<ClassStudentVo>> selectClassBySearchKey(
            @RequestParam(required = true) Integer courseId,
            @RequestParam(required = false) String className,
            @RequestParam(required = false) String createName,
            @RequestParam(required = false) Integer isAuthorized) {
        List<ClassStudentVo> classStudentVos = teacherService.selectClassBySearchKey(courseId, className, createName, isAuthorized);
        return R.success(classStudentVos);
    }

    @PostMapping("/addClassGrade")
    public R createClassroomCourseAuthorized(@RequestParam(required = true) String courseId,@RequestParam(required =true)String classId,@RequestParam(required =true)Boolean isAuthorized ){
        return teacherService.createClassroomCourseAuthorized(courseId,classId,isAuthorized);
    }
    }



